<template>
	<el-dialog v-model="isVisible" title="新增邀请" width="500px" :before-close="handleClose">
		<el-form ref="formRef" class="good-service-form" :model="form" label-width="120px" inline>
			<el-form-item label="短信文案" prop="message">
				<el-input type="textarea" v-model="form.message" disabled placeholder="输入短信文案" :maxlength="200" rows="8" show-word-limit />
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="handleClose">取消</el-button>
				<el-button type="primary" v-debounce="submit">发送短信</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="tsx">
import { FormRules, ElMessage } from 'element-plus';
import { ref, reactive, watch } from 'vue';
import { fetchSendMessage } from '/@/api/activity';

const props = defineProps({
	// 列表内容
	visible: {
		type: Boolean,
		default: () => false,
	},
	row: {
		type: Object,
		default: () => null,
	},
});

const emit = defineEmits(['handleClose']);

const isVisible = ref(false);

const formRef = ref();
const form = ref<any>({});
watch(
	() => props.visible,
	(val) => {
		isVisible.value = val;
		if (val) {
			form.value = {
				message: props.row.inviteSmsMessage,
			};
		}
	},
	{
		immediate: true,
	}
);

const submit = async () => {
	formRef.value.validate(async (valid: boolean) => {
		if (valid) {
			const res = await fetchSendMessage({ idList: props.row.idList });
			if (res.code == 200) {
				ElMessage.success('邀请短信已发送');
				handleClose(true);
			} else {
				ElMessage.error(res.msg);
			}
		}
	});
};

const handleClose = (success?: boolean) => {
	form.value = {
		order: 0,
	};
	formRef.value.resetFields();
	emit('handleClose', success === true);
};
</script>

<style lang="scss">
.good-service-form {
	.el-form-item {
		width: 90%;
		.el-input-number {
			width: 100%;
		}
	}
}
</style>
